import { _decorator, Component, EventMouse, EventTouch, Input, instantiate, Node, Prefab, Size, UITransform, Vec2, Vec3 } from 'cc';
import { Layer1 } from './Layer1';
import { GameState } from './GameState';
import { EventDispatcher } from './EventDispatcher';
import { EditorPanel } from './EditorPanel';
const { ccclass, property } = _decorator;

@ccclass('Editor')
export class Editor extends Component {
    @property({type: Prefab})
    gridMark: Prefab = null;
    @property(Layer1)
    layer1: Layer1 = null;
    @property(EditorPanel)
    editorPanel: EditorPanel = null;
    private currentGrid = null;
    initGrid() {
        const startX = -this.node.getComponent(UITransform).width / 2;
        const startY = this.node.getComponent(UITransform).height / 2;
        for(let i = 0; i < 15; i++){
            const x = startX + (i + 1) * 38.5;
           
            for(let j = 0; j < 17; j++){
                const y = startY - (j + 1) * 40;
                this.createGrid(x, y);
            }
        }
        this.node.on(Input.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.on(Input.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.on(Input.EventType.MOUSE_MOVE, this.onMouseMove, this);
    }

    onMouseMove(e: EventMouse) {
        const position = e.getUILocation();
        const localPosition = this.node.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(position.x, position.y));
        this.editorPanel.setPos(Math.floor(localPosition.x), Math.floor(localPosition.y));
    }

    protected onDestroy(): void {
        this.node.off(Input.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.off(Input.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.off(Input.EventType.MOUSE_MOVE, this.onMouseMove, this);
    }

    onTouchMove(e: EventTouch) {
        if(GameState.editMode !== 1) return;
        const position = e.getUILocation();
        const localPosition = this.node.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(position.x, position.y));
        for(let i = 0; i < this.node.children.length; i++){
            const grid = this.node.children[i]
            if(grid === this.currentGrid) continue;
            if(grid.getComponent(UITransform).getBoundingBox().contains(new Vec2(localPosition.x, localPosition.y))){
                this.layer1.addBlock(grid.getWorldPosition())
                this.layer1.refreshShadow();
                this.currentGrid = grid;
                break;
            }
        }
        EventDispatcher.getInstTarget().emit(EventDispatcher.UPDATE_BLOCK_SIZE);
    }   

    onTouchStart(e: EventTouch) {
        const position = e.getUILocation();
        const localPosition = this.node.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(position.x, position.y));
        switch(GameState.editMode){
            case 1:
                for(let i = 0; i < this.node.children.length; i++){
                    const grid = this.node.children[i]
                    if(grid.getComponent(UITransform).getBoundingBox().contains(new Vec2(localPosition.x, localPosition.y))){
                        this.layer1.addBlock(grid.getWorldPosition())
                        this.layer1.refreshShadow();
                    }
                }
                EventDispatcher.getInstTarget().emit(EventDispatcher.UPDATE_BLOCK_SIZE);
                break;
            case 2:
                this.layer1.removeBlock(new Vec3(position.x, position.y));
                this.layer1.refreshShadow();
                EventDispatcher.getInstTarget().emit(EventDispatcher.UPDATE_BLOCK_SIZE);
                break;
            case 3:
                this.createGrid(localPosition.x, localPosition.y, new Size(10, 10))
                break;
            case 4:
                for(let i = this.node.children.length - 1; i >= 0; i--){
                    const grid = this.node.children[i];
                    if(grid.getComponent(UITransform).getBoundingBox().contains(new Vec2(localPosition.x, localPosition.y))){
                        grid.removeFromParent();
                        break;
                    }
                }
                break;
        }
       
    }

    createGrid(x: number, y: number, size?: Size) {
        const grid = instantiate(this.gridMark);
        grid.setPosition(x, y);
        grid.setParent(this.node);
        if(size){
            grid.getComponent(UITransform).setContentSize(size);
        }
    }
}


